package controllers;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import database.dao.Impl.BusinessDaoImpl;
import database.dao.Impl.CustomerDaoImpl;
import entity.User;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Cursor;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextFormatter;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import services.LoginService;
import services.SystemContext;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.function.UnaryOperator;

/**
 * @ClassName LoginViewController
 * @Description TODO
 * @Author lzq
 * @Date 2021/6/15 15:18
 */
public class LoginViewController implements Initializable {


    public JFXButton LoginBtn;
    public JFXTextField userIdField;
    public JFXButton Registerbtn;
    public JFXPasswordField passwordField;
    public AnchorPane LoginPane;
    public CheckBox businesCheckBox;

    private double xOffset;
    private double yOffset;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        ViewManager.CONTROLLER.put("LoginViewController",this);

        Stage stage= ViewManager.STAGE.get("primaryStage");
        //拖放功能
        LoginPane.setOnMousePressed(event -> {
            xOffset = stage.getX() - event.getScreenX();
            yOffset = stage.getY() - event.getScreenY();
            LoginPane.setCursor(Cursor.CLOSED_HAND);
        });
        LoginPane.setOnMouseDragged(event -> {
            stage.setX(event.getScreenX() + xOffset);
            stage.setY(event.getScreenY() + yOffset);
        });

        LoginPane.setOnMouseReleased(event -> {
            LoginPane.setCursor(Cursor.DEFAULT);
        });

        //限制账号框只能输入数字
        userIdField.setTextFormatter(new TextFormatter<String>(new UnaryOperator<TextFormatter.Change>() {
            @Override
            public TextFormatter.Change apply(TextFormatter.Change change) {
                String value = change.getText();
                if(value.matches("[0-9]*")){
                    return change;
                }
                return null;
            }
        }));
    }

    /***
     * @Title:LoginAct
     * @Description:监听登录按钮
     * @param actionEvent
     * @return void
     * @Author: lzq
     */
    public void LoginAct(ActionEvent actionEvent) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                LoginService loginService=new LoginService();

                String userid=userIdField.getText();
                String password=passwordField.getText();
                User user=null;
                if(!businesCheckBox.isSelected()){
                    CustomerDaoImpl customerDao=new CustomerDaoImpl();
                    user=customerDao.selectCustomer(userid);

                }else{
                    BusinessDaoImpl businessDao=new BusinessDaoImpl();
                    user=businessDao.selectBusiness(userid);
                }
                if(user!=null&&!loginService.checkPassword(!businesCheckBox.isSelected(),user,password)){
                    Platform.runLater(()-> {
                        Alert alert = new Alert(Alert.AlertType.WARNING);
                        alert.setTitle("Warning!");
                        alert.setHeaderText("登录失败");
                        alert.setContentText("请重新检查你的账号与密码是否正确");
                        alert.showAndWait();
                    });
                }
                user.setPassword(password);
                SystemContext.getInstance().setUser(user);

            }
        }).start();
    }



    /***
     * @Title:RegisterAct
     * @Description:监听注册按钮
     * @param actionEvent
     * @return void
     * @Author: lzq
     */
    public void RegisterAct(ActionEvent actionEvent) {
        Stage registerStage=new Stage();
        ViewManager.STAGE.put("registerStage",registerStage);
        Parent root;
        try {
            root= FXMLLoader.load(getClass().getResource("/views/RegisterView.fxml"));
            registerStage.setTitle("注册窗口");
            registerStage.setScene(new Scene(root));
            registerStage.initStyle(StageStyle.UNDECORATED);
            registerStage.initModality(Modality.APPLICATION_MODAL);
            registerStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void closeAct(ActionEvent actionEvent) {
        Platform.exit();
        System.exit(0);
    }
}
